<?php
	require("libraries/student.php");
	class Games extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->model("model_game");
			$this->load->library("string");
			$this->load->helper("form");
			$this->load->library("form_validation");	
		}
		public function index(){
			$config['base_url'] = base_url().'admin/games/index/';
			$config['total_rows'] = $this->model_game->count_all();
			$data['title']= "Manage games";
			$data['template'] = "games/list";
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['data'] = "";
			$data['act'] = 4;
			$data['info']= $this->model_game->listall($config['per_page'],$start);
			$this->load->view("layout",$data);
		}
		public function add(){
			$data['error'] = "";
			$data['act'] = 4;
			$data['title'] = "Add new game";
			$data['template'] = "games/add";
			$data['listcate'] = $this->model_game->listcate();
			$data['listtags'] = $this->model_game->listtags();
			$data['data']['info'] = "";
			if($this->input->post("ok") != ""){
					  $url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('name'))));
					  $db = array(
							"name" => $this->input->post("name"),
							"rewrite" => $url,
							"order" => $this->input->post("order"),
							"author" => $this->input->post("author"),
							"source" => $this->input->post("source"),
							"keywords" => $this->input->post("keywords"),
							"descripts" => $this->input->post("descripts"),
							"shortinfo" => $this->input->post("shortinfo"),
							"hot" => $this->input->post("hot"),
							"tagid" => $this->input->post("tagid"),
							"cateid" => $this->input->post("cate_id")
						);
					if($_FILES['img']['name'] != NULL){
						$config['upload_path'] = './uploads/games';
						$config['allowed_types'] = 'jpg|jpeg|gif|png';
						$config['max_size']	= '2000';
						$config['max_width']  = '1024';
						$config['max_height']  = '1000';
						$this->load->library('upload',$config);
						if(!$this->upload->do_upload("img")){
							$data = array('error' => $this->upload->display_errors());
							$data['error'] = "";
							$data['title'] = "Add new game";
							$data['template'] = "games/add";
							$data['listcate'] = $this->model_game->listcate();
							$data['listtags'] = $this->model_game->listtags();
							$data['data']['info'] = "";
							$this->load->view("layout",$data);
							return FALSE;
						}else{
							$data = $this->upload->data();
							$db['image'] = $data['file_name'];
							//$this->createThumbnail($db['image'],"120","85");
						}
					 }
					 $this->model_game->add($db);
					 redirect(base_url()."admin/games");	
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$data['error'] = "";
			$data['act'] = 4;
			$data['title'] = "Edit game";
			$data['template'] = "games/edit";
			$data['listcate'] = $this->model_game->listcate();
			$data['listtags'] = $this->model_game->listtags();
			$data['info'] = $this->model_game->getdata($id);
			$data['data'] = "vl";
			$data['tagid']  = $data['info']['tagid'];
			$data['stt']  = $data['info']['cateid'];
			if($this->input->post("ok") != ""){
			  $url = str_replace(' ', '-',strtolower($this->string->replace($this->input->post('name'))));
			  $db = array(
					"name" => $this->input->post("name"),
					"rewrite" => $url,
					"order" => $this->input->post("order"),
					"author" => $this->input->post("author"),
					"source" => $this->input->post("source"),
					"keywords" => $this->input->post("keywords"),
					"descripts" => $this->input->post("descripts"),
					"shortinfo" => $this->input->post("shortinfo"),
					"hot" => $this->input->post("hot"),
					"tagid" => $this->input->post("tagid"),
					"cateid" => $this->input->post("cate_id")
				);
			if($_FILES['img']['name'] != NULL){
				$config['upload_path'] = './uploads/games';
				$config['allowed_types'] = 'jpg|jpeg|gif|png';
				$config['max_size']	= '2000';
				$config['max_width']  = '1024';
				$config['max_height']  = '1000';
				$this->load->library('upload',$config);
				if(!$this->upload->do_upload("img")){
					$data = array('error' => $this->upload->display_errors());
					$id = $this->uri->segment(4);
					$data['error'] = "";$data['act'] = 4;
					$data['title'] = "Edit game";
					$data['template'] = "games/edit";
					$data['listcate'] = $this->model_game->listcate();
					$data['listtags'] = $this->model_game->listtags();
					$data['info'] = $this->model_game->getdata($id);
					$data['data'] = "vl";
					$data['stt']  = $data['info']['cateid'];
					$this->load->view("layout",$data);
					return FALSE;
				}else{
					$data = $this->upload->data();
					$db['image'] = $data['file_name'];
					//$this->createThumbnail($db['customer_image'],"120","85");
				}
			 }
			 $this->model_game->update($db,$id);
			 redirect(base_url()."admin/games");	
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function del(){
			$id = $this->uri->segment(4);
			$data = $this->model_game->getdata($id);
			@unlink("uploads/games/".$data['image']);
			$this->model_game->del($id);
			redirect(base_url()."admin/games");
		}
		public function createThumbnail($fileName,$width,$height){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = 'uploads/games/'.$fileName;
			$config['new_image'] = 'uploads/games/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = $width;
			$config['height'] = $height;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}